package com.library.service;

import com.library.dao.BorrowDAO;
import com.library.dao.BookDAO;
import com.library.model.Book;

public class BorrowService {
    private BorrowDAO borrowDAO = new BorrowDAO();
    private BookDAO bookDAO = new BookDAO();

    public boolean borrowBook(String readerId, String bookId) {
        int count = borrowDAO.getBorrowCount(readerId);
        if (count >= 15) {
            return false;
        }

        Book book = bookDAO.getBookByNumber(bookId);
        if (book.getInventory() <= 0) {
            return false;
        }

        boolean result = borrowDAO.insert(readerId, bookId);
        if (result) {
            book.setInventory(book.getInventory() - 1);
            bookDAO.update(book);
        }
        return result;
    }
}